package com.twh.www.controller;
import com.twh.www.entity.WnGoods;
import com.twh.www.entity.WnUser;
import com.twh.www.service.MinioService;
import com.twh.www.service.WnCategoryService;
import com.twh.www.service.WnGoodsService;
import jakarta.servlet.http.HttpSession;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.ui.Model;
import java.math.BigDecimal;
import java.time.LocalDateTime;

@Controller
public class AdminController {
    @Autowired
    MinioService minioService;
    @Autowired
    WnCategoryService wnCategoryService;
    @Autowired
    WnGoodsService wnGoodsService;
    @GetMapping("/admin")
    public String admin(HttpSession session) {
        WnUser user = (WnUser) session.getAttribute("LoginUser");
        if( user== null ||!user.getAccount().equals("admin") ){
            return "redirect:/";//不是管理员不能进入
        }
        return "admin";
    }



    @GetMapping("/admin/logout")
    public String adminLogout(HttpSession session) {
        session.removeAttribute("LoginUser");
        return "redirect:/";
    }
    @PostMapping("/admin/upload/good")
    @ResponseBody
    public String uploadGoodImage(@RequestParam("file") MultipartFile file) throws Exception {
        // 校验格式
        String fileName = file.getOriginalFilename();
        if (fileName == null || !(fileName.endsWith(".jpg") || fileName.endsWith(".png"))) {
            return "图片格式仅支持jpg/png";
        }
        // 校验大小
        if (file.getSize() > 5 * 1024 * 1024) {
            return "图片大小不能超过2M";
        }
        // 上传到MinIO
        String imageUrl = minioService.uploadFile(file);
        return imageUrl;
    }

    @GetMapping("/admin/goods/add")
    public String addGoods(Model model) {
        model.addAttribute("categories", wnCategoryService.list());
        return "goods_add";
    }

    @PostMapping("/admin/goods/add")
    @ResponseBody
    public String addGoods(
            @RequestParam("name") String name,
            @RequestParam("no") String no,
            @RequestParam("categoryId") Integer categoryId,
            @RequestParam("stock") Integer stock,
            @RequestParam("marketPrice") BigDecimal marketPrice,
            @RequestParam("salePrice") BigDecimal salePrice,
            @RequestParam("imageUrl") String imageUrl,
            @RequestParam("description") String description,
            @RequestParam(value = "hottest", required = false) String hottest,
            @RequestParam(value = "newest", required = false) String newest
    ) {
        WnGoods goods = new WnGoods();
        goods.setName(name);
        goods.setNo(no);
        goods.setCategoryId(categoryId);
        goods.setStock(stock);
        goods.setMarketPrice(marketPrice);
        goods.setSalePrice(salePrice);
        goods.setImage(imageUrl);
        goods.setDescription(description);
        goods.setHottest("y".equalsIgnoreCase(hottest) ? "Y" : "N");
        goods.setNewest("y".equalsIgnoreCase(newest) ? "Y" : "N");
        goods.setStatus("1"); // 正常上架
        goods.setSaleTime(LocalDateTime.now());
        wnGoodsService.save(goods);
        return "success";
    }
}
